﻿/*
 * jsmsg.cpp
 *
 *  Created on: 2009-7-8
 *      Author: root
 */

#include "jsmsg.h"

int CreateType(int flag,int modID,int subModID,int messageID)
{
    int type;
    INITTYPE(type);
    if(flag)
    {
        SETTYPEFLAG(type);
    }
    else
    {
        RESETTYPEFLAG(type);
    }
    PUTMODID(type,modID);
    PUTSUBMODID(type,subModID);
    PUTMESSAGEID(type,messageID);

    return type;
}

void GetType(int type,int *flag,int *modID,int *subModID,int *messageID)
{
    *flag = GETTYPEFLAG(type);
    *modID = GETMODID(type);
    *subModID = GETSUBMODID(type);
    *messageID = GETMESSAGEID(type);
}

CJSMsg::CJSMsg(void)
{
    m_pByteArray = new CJSByteArray;
    if (m_pByteArray == NULL)
    {
        return;
    }
}
CJSMsg::CJSMsg(const CJSMsg& msg)
{
    this->m_pByteArray = new CJSByteArray(*(msg.m_pByteArray));
}

CJSMsg::CJSMsg(int type, int len)
{
    if (len < 0)
    {
        return;
    }

    m_pByteArray = new CJSByteArray(len+8);
    if (m_pByteArray == NULL)
    {
        return;
    }

    //填冲类型
    if (m_pByteArray->PutInt(type) == -1)
    {
        return;
    }

    //填冲长度
    if (m_pByteArray->PutInt(len) == -1)
    {
        return;
    }
}

CJSMsg::~CJSMsg(void)
{
    if (m_pByteArray != NULL)
    {
        delete m_pByteArray;
        m_pByteArray = NULL;
    }
}

CJSByteArray* CJSMsg::GetByteArray(void)
{
    return m_pByteArray;
}

//放入数据包
int CJSMsg::PutMsg(char str[], int nLen)
{
    return m_pByteArray->PutData(str, nLen);
}

//取得数据包
char* CJSMsg::GetMsg(void)
{
    return m_pByteArray->GetData();
}


//取数据包的类型(TLV. 取T)
int CJSMsg::Type(void)
{
    return m_pByteArray->GetT();
}

//取数据包的长度(V的长度, 取L)
int CJSMsg::Length(void)
{
    return m_pByteArray->GetL();
}

void CJSMsg::UnPackBegin(void)
{
    return m_pByteArray->UnpackBegin();
}

int CJSMsg::MsgLength(void)
{
    return m_pByteArray->Length();
}


int CJSMsg::Clear(void)
{
    return m_pByteArray->Clear();
}


int CJSMsg::End(void)
{
    return m_pByteArray->End();
}
CJSMsg& CJSMsg::operator=(const CJSMsg& msg)
{
    this->m_pByteArray = new CJSByteArray(*(msg.m_pByteArray));
    return *this;
}



